import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter_app_update/flutter_app_update.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/apis/version_api.dart';
import 'package:sainbainu/common/models/request/pager_request.dart';
import 'package:sainbainu/common/models/response/version_list_response.dart';
import 'package:sainbainu/common/repositories/version_repository.dart';
import 'package:sainbainu/common/services/app_update.dart';
import 'package:sainbainu/common/utils/util.dart';

import 'index.dart';

class VersionController extends GetxController {
  VersionController();

  final state = VersionState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() async {
    super.onInit();

    state.refreshController = EasyRefreshController(
      controlFinishRefresh: false,
      controlFinishLoad: true,
    );
    state.versionRepository = VersionRepository(VersionApi());
    state.pagerRequest = PagerRequest();

    state.localVersionCode = await AppUpdateService.to.localVersionCode;
    getList();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.refreshController.dispose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }

  /// 获取内容列表
  Future getList({bool refresh = true}) async {
    if (refresh) {
      state.pagerRequest.pageNum = 1;
    }
    var res = await state.versionRepository.versionList(
      state.pagerRequest,
      Util.getCurrentPlatformType(),
    );
    if (state.localVersionCode != null) {
      for (var item in res.rows) {
        int? serviceVersionCode = int.tryParse(item.versionNo ?? '');
        var canUpdate = Util.chechVersionCode(
          state.localVersionCode!,
          serviceVersionCode,
        );
        item.canUpdate = canUpdate;
        item.curVersion = state.localVersionCode == serviceVersionCode;
      }
    }

    if (refresh) {
      state.versionListResponse.rows = res.rows;
    } else {
      state.versionListResponse.rows.addAll(res.rows);
    }

    state.versionListResponse.total = res.total;
    state.hasMore =
        state.pagerRequest.pageNum * state.pagerRequest.pageSize < res.total;
    state.pagerRequest.pageNum++;
    update(['list']);
  }

  /// 上拉下载
  Future onRichBottom() async {
    try {
      if (state.hasMore) {
        await getList(refresh: false);
      }
      if (!state.hasMore) {
        state.refreshController.finishLoad(IndicatorResult.noMore);
      } else {
        state.refreshController.finishLoad(IndicatorResult.success);
      }
    } catch (e) {
      state.refreshController.finishLoad(IndicatorResult.fail);
    }
  }

  /// 更新app
  onUpdateVersion(VersionItemResponse item) {
    UpdateModel model = UpdateModel(
      item.appLink ?? '',
      "SainBainu-v${item.versionName}.apk",
      "ic_launcher",
      'https://apps.apple.com/cn/app/抖音/id1142110895',
    );
    AzhonAppUpdate.update(model);
  }
}
